;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;                                                                           ;;
;;                     Unofficial Chinese Clone Support                      ;;
;;                      http://code.google.com/p/uccs/                       ;;
;;                                                                           ;;
;;                            Version 0.21                                   ;;
;;                                                                           ;;
;;                            by Tomas Rimkus                                ;;
;;             (based on work by Brian Jorgensen, Leon and Micha)            ;;
;;                                                                           ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; Script Based on..: DLLCall: Support for Human Interface devices
; By...............: Micha
; URL..............: http://www.autohotkey.com/forum/viewtopic.php?t=6367

SendMode Input

; Set screen title, to set the HWND
Gui, Show, x0 y0 h0 w0, FnMapper

; Variable for the modifier key
FnKeyPressed := false

; Set the homepath to the relevant dll file
HomePath=AutohotkeyRemoteControl.dll

; Load the dll
hModule := DllCall("LoadLibrary", "str", HomePath)

; On specific message from the dll, goto this function
OnMessage(0x00FF, "InputMsg")

; Register at the dll in order to receive events
EditUsage := 1
EditUsagePage := 12
HWND := WinExist("FnMapper")
nRC := DllCall("AutohotkeyRemoteControl\RegisterDevice", INT, EditUsage, INT
, EditUsagePage, INT, HWND, "Cdecl UInt")
WinHide, FnMapper

; This function is called, when a WM_INPUT-msg from a device is received
InputMsg(wParam, lParam, msg, hwnd) 
{
  DeviceNr = -1
  nRC := DllCall("AutohotkeyRemoteControl\GetWM_INPUTDataType", UINT, wParam
  , UINT, lParam, "INT *", DeviceNr, "Cdecl UInt")
  if (errorlevel <> 0) || (nRC == 0xFFFFFFFF) 
  {
  	MsgBox GetWM_INPUTHIDData fehlgeschlagen. Errorcode: %errorlevel%
  	; If there was an error retrieving the HID data, cleanup
	DllCall("FreeLibrary", "UInt", hModule)
	ExitApp
  }
  ;Tooltip, %DeviceNr%
  ifequal, nRC, 2
  {
    ProcessHIDData(wParam, lParam)
  }
  else 
  {
  	MsgBox, Error - no HID data
  }
}
Return

ProcessHIDData(wParam, lParam)
{
  DataSize = 5000
  VarSetCapacity(RawData, %DataSize%, 0)
  RawData = 1
  nHandle := DllCall("AutohotkeyRemoteControl\GetWM_INPUTHIDData", UINT, wParam
  , UINT, lParam, "UINT *" , DataSize, "UINT", &RawData, "Cdecl UInt")

  loop, %DataSize%
  {
    Zahl := NumGet(RawData, a_index-1,"UChar")
    Zahl := Dez2Hex(Zahl)    
    Vals = %Vals%%Zahl%     
  }
  
  if (Vals = 0502 || Vals = 0503)
	Return
  
  global VolumeControlsAreDefault, MediaControlsAreDefault, RectangleEjects
  if (Vals = 02000002)
	MediaKeyCall("VolumeMute", "VolumeControlsAreDefault", "F10")
  else if (Vals == 02000001)
	MediaKeyCall("VolumeDown", "VolumeControlsAreDefault", "F11")
  else if (Vals == 02008000)
	MediaKeyCall("VolumeUp", "VolumeControlsAreDefault", "F12")
  else if (Vals == 02000800)
	MediaKeyCall("MediaCommandPrev", "MediaControlsAreDefault", "F7")
  else if (Vals == 02004000)
	MediaKeyCall("MediaCommandPlay", "MediaControlsAreDefault", "F8")
  else if (Vals == 02001000)
	MediaKeyCall("MediaCommandNext", "MediaControlsAreDefault", "F9")
  else if (Vals == 02020000)
	Send {PrintScreen}
  else if (Vals == 02080000)
	if (!RectangleEjects)
		Send {Browser_Home}
	else
		Drive Eject
  else if (Vals == 02040000)
  {
	Hotkey $*LWin, Off, UseErrorLevel
	Hotkey $*LWin Up, Off, UseErrorLevel
	RunWait %A_WinDir%\system32\osk.exe
	Hotkey $*LWin, On, UseErrorLevel
	Hotkey $*LWin Up, On, UseErrorLevel
  }
  else if (Vals == 02400000)
	if (IsVistaplus())
		Send ^{Esc}
	else
		Send #f
  else if (Vals == 02002000)
	Run rundll32.exe user32.dll`,LockWorkStation
}

MediaKeyCall(normalAction, pref, prefKey)
{
	global
	if (%pref%)
		Send {Blind}{%prefKey%}
	else
		%normalAction%()
}

Dez2Hex(Number)
{
    format = %A_FormatInteger%    ; save original integer format
    SetFormat Integer, Hex        ; for converting bytes to hex
    Number += 0
    SetFormat Integer, %format%   ; restore original format
    StringTrimLeft, Number, Number, 2
    Stringlen := StrLen(Number)
    if Stringlen < 2
    Number = 0%Number%
    return Number
}

IsVistaPlus() 
{
   Return ((DllCall("GetVersion") & 0xFF) > 5)
}
